//
//  ChangeParkListViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/8.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ChangeParkListViewController.h"
#import "ChangeParkTableViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "ChangeChipListDetailViewController.h"

@interface ChangeParkListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) MetaModel *meta;

@end

@implementation ChangeParkListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    [self setupView];
    [self loadData];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_chip_change"} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            weakself.meta = meta;
        }
        [weakself.tableView reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)loadMoreData {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_chip_change"} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        weakself.meta = meta;
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            [weakself.tableView.mj_footer endRefreshing];
        }
        [weakself.tableView reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        [weakself.tableView.mj_footer endRefreshing];
    }];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"芯片安装任务";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"ChangeParkTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChangeParkTableViewCell"];
    _tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 79;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChangeParkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeParkTableViewCell" forIndexPath:indexPath];
    NSDictionary *dic = _array[indexPath.row];
    cell.label1.text = [NSString stringWithFormat:@"%@", dic[@"parking"][@"name"]];
    cell.label2.text = [NSString stringWithFormat:@"低电预警 (%@)", dic[@"rfids_nums"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChangeChipListDetailViewController *con = [[ChangeChipListDetailViewController alloc] init];
    con._id = [_array[indexPath.row][@"parking"][@"id"] intValue];
    [self.navigationController pushViewController:con animated:YES];
}

@end
